home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PsL Monthly 1993 December
/
PSL Monthly Shareware CD-ROM (December 1993).iso
/
prgmming
/
dos
/
c
/
rs232.exe
/
RS232.DOC
< prev
Wrap
Text File
|
1993-05-03
|
47KB
|
1,083 lines
RS232.C User Documentation
copyright C. Karcher 1992,93
> About RS232.C:
RS232.C was written to provide all of the basic functionality needed
to employ serial I/O in any application written with Borland 'C'
language compilers. Some features are:
1. Ease of use. No assembly language or library files are used and a
simple "#include" statement is all that is required to access all of
the functions provided.
2. Both input and output are buffered and interrupt driven for
efficiency.
3. Serial ports 1 - 4 are supported on PC, AT and PS/2 compatibles.
Chained interrupts used on port 3 and 4 are allowed for so as not to
interfere with devices such as a mouse or printer. Transmission
speeds of 110 to 115200 baud are available.
4. Detection and utilization of hardware buffered UARTs (NS16550AF
etc.) found in some machines is automatic.
5. Interrupt driven hardware and XON/XOFF flow control is provided for.
6. All source code is included. RS232.C can be used with all memory
models.
This is user supported software and may be distributed freely in a
whole and unmodified state. It may be used in part or wholly, free of
royalty, to develop any commercial applications provided that the
developer has registered RS232.C. Registration entitles the developer
to any future enhancements or upgrades which may be released.
Registration also includes technical support as far as can be provided
via mail, telephone or electronic messaging. To register RS232.C,
send a check or money order for $20.00 to:
Chris A. Karcher
9537 Evanston Ave. N.
Seattle WA 98103-3131
The author may be contacted by mail at the above address, by telephone
at (206)789-7945 or via Compuserve EMAIL to user ID 76406,536.
The author of RS232.C makes no expressed or implied warranty of any
kind with regard to the software or accompanying documentation. In no
event shall the author be held liable for incidental or consequential
damages in connection or arising out of performance or use of any part
of this software.
The distribution should include the following files:
RS232.C - serial communication routines source code
RS232.DOC - documentation (this file)
RS_DEMO.C - sample program demonstrating RS232.C
> Why use RS232.C?
Borland's C compilers (and probably all DOS based C compilers) do
provide some basic support for serial communications but that support
makes use of calls to BIOS routines only. The BIOS serial routines
are adequate only for the slowest of transmission speeds and do not
exploit all of the capabilities of the hardware found in PC's. If
efficient serial communication is required, the programmer must
provide all of the low level software. RS232.C does just that by
taking care of all the low level details and providing the programmer
with all the high level functions needed for efficient serial I/O.
> How to use RS232.C:
All that is required to use RS232.C is to include it in your source
code. The simplest way to do that is with a compiler '#include'
directive, treating RS232.C just as you would a header file. Having
done that, three basic steps are all that are required to perform
serial I/0:
1. Allocate memory for input and output buffers. (It can be pre-
allocated at compile time or allocated via some memory
allocation function like malloc at runtime.)
2. "Open" the port with the function rs_initport, using the
desired communication parameters.
perform I/O with functions provided
. . . . .
. . . . .
. . . . .
3. "Close" the port when finished performing I/O with the
function rs_close before ending the program.
The file RS_DEMO.C, included with the distribution, is a sample
terminal program demonstrating most of the functions available with
RS232.C.
The following code demonstrates an extremely simple but functional
program which turns a PC into a dumb terminal capable of communicating
with a modem or another computer:
/*
TERM: a light weight dumb terminal program to demonstrate rs232.c
C. Karcher
*/
#include<conio.h>
#include"src\rs232.c" /* include rs232 variables and functions */
main()
{
int key = 0;
char input_buffer[1024],output_buffer[1024]; /* allocate buffers */
/* open COM port 1 with 2400 baud, no parity, 8 data bits, 1 stop bit,
a 1024 byte input buffer and a 1024 byte output buffer */
if(rs_initport(RS_PORT1,RS_B2400,RS_NOPAR,RS_DBIT8,RS_SBIT1,
1024U,input_buffer,1024U,output_buffer) > 0)
cprintf("Terminal mode active - press escape to exit\r\n");
else{
cprintf("Unable to open COM port\r\n");
return 0;
}
/* turn on Data Terminal Ready */
rs_modctrl(RS_WRTMCR,RS_MCRDTR,RS_LINON);
/* display any characters received and send any keystrokes typed until
escape is pressed */
do{
if(rs_inrcvd()) /* if a character has been received */
putch(rs_getbyt()); /* display it */
if(rs_keyhit()) /* if a key has been pressed */
rs_sndbyt((key = getch())); /* send it */
}while(key != 27); /* if the key pressed was ESC, end */
/* turn off Data Terminal Ready */
rs_modctrl(RS_WRTMCR,RS_MCRDTR,RS_LINOFF);
/* close the port and exit */
rs_close();
return 0;
} /* end TERM */
> RS232.C Functions:
The following is a list of the functions contained in RS232.C and a
brief explanation of the purpose of each one. A detailed description
of each function and it's use follows:
rs_initport - Prepares a COM port for use.
rs_sndbyt - Sends a single byte.
rs_sndstr - Sends a string of bytes.
rs_getbyt - Gets a single byte.
rs_getstr - Gets a string of bytes.
rs_inrcvd - Returns number of bytes which been received.
rs_outfre - Returns amount of free space remaining in output buffer.
rs_error - Returns code for last error detected.
rs_modctrl - Controls or returns status of modem control lines.
rs_break - Sends break to remote equipment.
rs_clrout - Clears output buffer.
rs_clrin - Clears input buffer.
rs_keyhit - Determines if a key has been pressed.
rs_timer - Times events or operations.
rs_setflow - Establishes or returns status of flow control.
rs_close - "Closes" port prepared by rs_initport.
> Function Reference: (Note: The pre defined constants shown in
parentheses with some of the function argument
descriptions or return value descriptions are
defined in RS232.C and may be used for
convenience.)
> rs_initport:
> Purpose:
Prepares a COM port for use.
> Prototype:
int rs_initport(char port, long baud, char parity, char data_bits,
char stop_bits, unsigned in_buf_size, char *in_buf,
unsigned out_buf_size, char *out_buf);
> Arguments:
port: Character indicating which port to use. May be one of
the following:
'1' (RS_PORT1) - COM port 1
'2' (RS_PORT2)
'3' (RS_PORT3)
'4' (RS_PORT4) - COM port 4
baud: The long value indicating the baud rate. Can be any of
the following:
110L (R